const main = require('../main/main');
const datbase = require('../main/datbase')

describe('pos', function () {
    var allItems;
    var inputs;

    beforeEach(function () {
        allItems = datbase.loadAllItems();
        inputs = [
            'ITEM000001',
            'ITEM000001',
            'ITEM000001',
            'ITEM000001',
            'ITEM000001',
            'ITEM000003-2',
            'ITEM000005',
            'ITEM000005',
            'ITEM000005'
        ];
    });

    it('should print correct text', function () {

        spyOn(console, 'log');                      
        main.printInventory(inputs);

        var expectText =
            '***<没钱赚商店>购物清单***\n' +
            '名称：雪碧，数量：5瓶，单价：3.00(元)，小计：12.00(元)\n' +
            '名称：荔枝，数量：2斤，单价：15.00(元)，小计：30.00(元)\n' +
            '名称：方便面，数量：3袋，单价：4.50(元)，小计：9.00(元)\n' +
            '----------------------\n' +
            '挥泪赠送商品：\n' +
            '名称：雪碧，数量：1瓶\n' +
            '名称：方便面，数量：1袋\n' +
            '----------------------\n' +
            '总计：51.00(元)\n' +
            '节省：7.50(元)\n' +
            '**********************';

        expect(console.log).toHaveBeenCalledWith(expectText);
    });

    it('1',function(){
        let result=main.getobitem(inputs);
        expect(result).toEqual([{ barcode: 'ITEM000001', count: 5 },{ barcode: 'ITEM000003', count: 2 },{ barcode: 'ITEM000005', count: 3 }]);
    });

    it('2',function(){
        let obitem=main.getobitem(inputs);
        let result=main.addattribute(obitem);
        expect(result).toEqual([ Object({ barcode: 'ITEM000001', count: 5, name: '雪碧', unit: '瓶', price: 3 }), Object({ barcode: 'ITEM000003', count: 2, name: '荔枝', unit: '斤', price:
        15 }), Object({ barcode: 'ITEM000005', count: 3, name: '方便面', unit: '袋', price: 4.5 }) ]);
    });

    it('3',function(){
        let obitem=main.getobitem(inputs);
        main.addattribute(obitem);
        let result=main.addprice(obitem);
        expect(result).toEqual([ Object({ barcode: 'ITEM000001', count: 5, name: '雪碧', unit: '瓶', price: 3, discount: 1, Actual_count: 4, total_price: 12, distotal_price: 3 }), Object({
            barcode: 'ITEM000003', count: 2, name: '荔枝', unit: '斤', price: 15, discount: 0, Actual_count: 2, total_price: 30, distotal_price: 0 }), Object({ barcode: 'ITEM000005',
            count: 3, name: '方便面', unit: '袋', price: 4.5, discount: 1, Actual_count: 2, total_price: 9, distotal_price: 4.5 }) ]);
    });

    it('4',function(){
        let obitem=main.getobitem(inputs);
        main.addattribute(obitem);
        main.addprice(obitem);
        let result=main.getdiscount_array(obitem);
        expect(result).toEqual([ Object({ barcode: 'ITEM000001', count: 5, name: '雪碧', unit: '瓶', price: 3, discount: 1, Actual_count: 4, total_price: 12, distotal_price: 3 }), Object({
            barcode: 'ITEM000005', count: 3, name: '方便面', unit: '袋', price: 4.5, discount: 1, Actual_count: 2, total_price: 9, distotal_price: 4.5 }) ]);
    });

    it('5',function(){
        let obitem=main.getobitem(inputs);
        main.addattribute(obitem);
        main.addprice(obitem);
        let result=main.getall_price(obitem);
        expect(result).toEqual(51);
    });

    it('6',function(){
        let obitem=main.getobitem(inputs);
        main.addattribute(obitem);
        main.addprice(obitem);
        let discount_array=main.getdiscount_array(obitem);
        let result=main.getalldis_price(discount_array);
        expect(result).toEqual(7.5);
    });
});
